package com.huanying.goodjob.skill;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;
import com.huanying.framework.utils.MemCachedManager;
import com.huanying.goodjob.jobtype.Jobtype;
import com.huanying.goodjob.jobtype.JobtypeService;

@Controller
public class SkillController extends BaseController{
	private static Logger logger = Logger.getLogger(SkillController.class);
	@Autowired
	private SkillService skillService;
	@Autowired
	private JobtypeService jobtypeService;
	 private MemCachedManager cache ;
	 //查询技能
	@RequestMapping("/show_add_skill.do")
	public String show_add_skill(String jobtype, String name, String page_num, Model model) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		map.put("jobtype", jobtype);
		map.put("name", name);
		if (page_num == null) {
			page_num = "1";
		}
		try {
			pageBean = skillService.search_skill(map, super.getPageSize(), Integer.valueOf(page_num));
		} catch (NumberFormatException e) {
			logger.error("查询技能失败", e);
			// TODO Auto-generated catch block
		} catch (Exception e) {
			logger.error("查询技能失败", e);
			// TODO Auto-generated catch block
		}
		model.addAttribute("jobtype", jobtype);
		model.addAttribute("name", name);
		model.addAttribute("skills", pageBean.getList());
		model.addAttribute("page", pageBean);
		model.addAttribute("pageSize", pageBean.getPageSize());
		return "/skill/add_skill";
	}
//添加技能
	@RequestMapping(value = "/add_skill.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add_skill(@RequestBody Skill skill, String jobtype_id, String status) {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			Jobtype jobtype = jobtypeService.getbyId_jobtype(Integer.parseInt(jobtype_id));
			skill.setJobtype(jobtype);
			// 判断工种的状态如果状态为1将添加的技能的状态也至为1，否则为0
			if (skill.getId() == 0) {
				if (jobtype.getStatus() == 1) {
					skill.setStatus(1);
				} else {
					skill.setStatus(0);
				}
				skillService.add_skill(skill);
			} else {
				skill.setStatus(Integer.parseInt(status));
				skillService.update_skill(skill);
			}
			map.put("flag", "1");
			map.put("id", String.valueOf(skill.getId()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			map.put("flag", "0");
			logger.error("添加技能失败", e);
		}
		return map;
	}
//后台获取全部的工种
	@RequestMapping("/get_jobtype.do")
	@ResponseBody
	public Map<String, Object> get_jobtype() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Jobtype> list = new ArrayList<Jobtype>();
		try {
			list = skillService.get_jobtype();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取工种失败", e);
		}
		map.put("flag", "1");
		map.put("jobtype", list);
		return map;
	}
	//后台获取单个技能
	@RequestMapping("/get_skill.do")
	@ResponseBody
	public Map<String, Object> get_skill(int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		Skill skill = null;
		try {
			skill = skillService.getbyId_skill(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取技能失败", e);
		}
		map.put("flag", "1");
		map.put("skill", skill);
		return map;
	}
//后台删除技能
	@RequestMapping("/delete_skill.do")
	@ResponseBody
	public Map<String, Object> delete_skill(int id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int flog = 0;
		try {
			flog = skillService.delete_skill(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("删除技能失败", e);
		}
		map.put("flag", flog);
		return map;
	}
	
	//前台获取技能
		@RequestMapping(value = "/get_skillbyjobtype.do", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
		@ResponseBody
		public String get_skillbyjobtype(String id,String jsoncallback){
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String,Object> mapcache=new HashMap<String,Object>();
			List<Skill> realskills =new ArrayList<Skill>();
			cache=MemCachedManager.getInstance();
			if(cache.get("skill")!=null)
			{
				mapcache=(Map<String,Object>)cache.get("skill");
				Iterator<Map.Entry<String, Object>> entries = mapcache.entrySet().iterator();  
				  
				while (entries.hasNext()) {  
				  
					Map.Entry<String, Object> entry =entries.next();  
				    if(((Skill)entry.getValue()).getJobtype().getId()==Integer.parseInt(id) && ((Skill)entry.getValue()).getStatus()==1)
				    {
				    	realskills.add((Skill)entry.getValue());
				    }
				    	map.put("skills",realskills);
			}
			}
			else{
			try {
				realskills=skillService.search__skillbyjobtypeId(Integer.parseInt(id));
				map.put("skills",realskills);
			} catch (Exception e) {
				// TODO Auto-generated catch block
			logger.error("前台获取技能失败", e);
			}	
		}
			Gson gson=new Gson();
			String str=gson.toJson(map);
			return  jsoncallback+"("+str+")";
			}
}
